home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / rclesrc.10 / ircle sources / IRCPreferences.p < prev    next >
Encoding:
Text File  |  1992-09-06  |  7.7 KB  |  315 lines

  1. {    ircle - Internet Relay Chat client    }
  2. {    File: IRCPreferences    }
  3. {    Copyright ⌐ 1992 Olaf Titz (s_titz@iravcl.ira.uka.de)    }
  4.  
  5. {    This program is free software; you can redistribute it and/or modify    }
  6. {    it under the terms of the GNU General Public License as published by    }
  7. {    the Free Software Foundation; either version 2 of the License, or    }
  8. {    (at your option) any later version.    }
  9.  
  10. {    This program is distributed in the hope that it will be useful,    }
  11. {    but WITHOUT ANY WARRANTY; without even the implied warranty of    }
  12. {    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    }
  13. {    GNU General Public License for more details.    }
  14.  
  15. {    You should have received a copy of the GNU General Public License    }
  16. {    along with this program; if not, write to the Free Software    }
  17. {    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.    }
  18.  
  19. unit IRCPreferences;
  20. { deals with the Preferences settings and files }
  21.  
  22. interface
  23. uses
  24.     TCPTypes, TCPStuff, TCPConnections, ApplBase, MsgWindows, IRCGlobals;
  25.  
  26. procedure InitIRCPreferences;
  27. { startup }
  28.  
  29. function GetPrefs (request: boolean): boolean;
  30. { get prefs from file (request=false)/ inquire (request=true) }
  31.  
  32. procedure GetShortcuts;
  33. { inquire new shortcuts }
  34.  
  35. implementation
  36.  
  37. {$SETC DEBUG=false}
  38.  
  39. const
  40.     MAXMENU = 16;
  41.  
  42. var
  43.     shcd: DialogPtr;
  44.  
  45. function NewClearHandle (nbytes: longint): Handle;
  46. inline
  47.     $201F,        {    MOVE.L (A7)+, D0        }
  48.     $A322,        {    _NewHandle,Clear        }
  49.     $2E88,        {    MOVE.L A0,(A7)        }
  50.     $31C0, $0220;    {    MOVE.W D0,MemError    }
  51.  
  52. procedure defTODia (d: DialogPtr);
  53.     var
  54.         h: Handle;
  55.         i, j: integer;
  56.         l: longint;
  57.         s: Str255;
  58.         r: Rect;
  59.     begin
  60.         GetDItem(d, 3, i, h, r);
  61.         SetIText(h, default^^.Server);
  62.         GetDItem(d, 4, i, h, r);
  63.         NumToString(default^^.Port, s);
  64.         SetIText(h, s);
  65.         GetDItem(d, 5, i, h, r);
  66.         SetIText(h, default^^.Nick);
  67.         GetDItem(d, 6, i, h, r);
  68.         SetIText(h, default^^.userLoginName);
  69.         GetDItem(d, 7, i, h, r);
  70.         SetIText(h, default^^.userName);
  71.         GetDItem(d, 8, i, h, r);
  72.         SetIText(h, default^^.autoExec);
  73.         for i := 1 to 4 do begin
  74.             GetDItem(d, i + 8, j, h, r);
  75.             SetCtlValue(ControlHandle(h), ord(default^^.notify[i]))
  76.         end;
  77.     end;
  78.  
  79. procedure defFROMDia (d: DialogPtr);
  80.     var
  81.         h: Handle;
  82.         i, j: integer;
  83.         l: longint;
  84.         s: Str255;
  85.         r: Rect;
  86.     begin
  87.         GetDItem(d, 3, i, h, r);
  88.         GetIText(h, default^^.Server);
  89.         GetDItem(d, 4, i, h, r);
  90.         GetIText(h, s);
  91.         StringToNum(s, l);
  92.         default^^.Port := loword(l);
  93.         GetDItem(d, 5, i, h, r);
  94.         GetIText(h, default^^.Nick);
  95.         GetDItem(d, 6, i, h, r);
  96.         GetIText(h, default^^.userLoginName);
  97.         GetDItem(d, 7, i, h, r);
  98.         GetIText(h, default^^.userName);
  99.         GetDItem(d, 8, i, h, r);
  100.         GetIText(h, default^^.autoExec);
  101.         for i := 1 to 4 do begin
  102.             GetDItem(d, i + 8, j, h, r);
  103.             default^^.notify[i] := boolean(GetCtlValue(ControlHandle(h)))
  104.         end;
  105.     end;
  106.  
  107. procedure SetShortcutsMenu;
  108.     var
  109.         m: MenuHandle;
  110.         i: integer;
  111.         s: str255;
  112.     begin
  113.         m := GetMHandle(261);
  114.         for i := 1 to 10 do begin
  115.             if Shortcuts^^[i] = '' then
  116.                 s := ' '
  117.             else if length(Shortcuts^^[i]) > MAXMENU then
  118.                 s := concat(copy(Shortcuts^^[i], 1, MAXMENU), '...')
  119.             else
  120.                 s := Shortcuts^^[i];
  121.             SetItem(m, i + 2, s);
  122.         end;
  123.     end;
  124.  
  125. function CloseShortcuts (var e: eventRecord): boolean;
  126.     var
  127.         i, j: integer;
  128.         h: Handle;
  129.         r: Rect;
  130.     begin
  131.         if shcd <> nil then
  132.             if DialogPtr(e.message) = shcd then
  133.                 if TrackGoAway(shcd, e.where) then begin
  134.                     for i := 1 to 10 do begin
  135.                         GetDItem(shcd, i, j, h, r);
  136.                         GetIText(h, Shortcuts^^[i]);
  137.                         if Shortcuts^^[i] = ' ' then
  138.                             Shortcuts^^[i] := '';
  139.                     end;
  140.                     SetShortcutsMenu;
  141.                     DisposDialog(shcd);
  142.                     shcd := nil;
  143.                     ChangedResource(Handle(shortcuts));
  144.                     WriteResource(Handle(shortcuts));
  145.                     CloseShortcuts := true;
  146.                     exit(CloseShortcuts)
  147.                 end;
  148.         CloseShortcuts := false
  149.     end;
  150.  
  151. procedure savefont;
  152.     begin
  153.         with defFont^^ do begin
  154.             fnt := MWdefaultFont;
  155.             siz := MWdefaultSize
  156.         end;
  157.         ChangedResource(Handle(defFont));
  158.         WriteResource(Handle(defFont));
  159.     end;
  160.  
  161. procedure InitIRCPreferences;
  162.     var
  163.         i, j: integer;
  164.         a: AppFile;
  165.         s: string;
  166.         p: Point;
  167.         t: SFTypeList;
  168.         r: SFReply;
  169.     begin
  170.         ValidPrefs := false;
  171.         shcd := nil;
  172. {$IFC DEBUG}
  173.         if SetVol(nil, 0) = 0 then begin
  174.             i := OpenResFile('ircleTest');
  175.             if ResError = 0 then
  176.                 exit(InitIRCPreferences);
  177.         end;
  178. {$ENDC}
  179.         CountAppFiles(i, j);
  180.         if j = 0 then begin
  181.             repeat
  182.                 s := NewFileName('Save Preferences as:');
  183.                 if s = '' then begin
  184.                     SetPt(p, 80, 30);
  185.                     t[0] := 'Pref';
  186.                     SFGetFile(p, '', nil, 1, t, nil, r);
  187.                     if r.good then
  188.                         if SetVol(nil, r.vRefNum) <> 0 then
  189.                             cycle;
  190.                     i := OpenResFile(r.fName);
  191.                 end
  192.                 else begin
  193.                     if Create(s, 0, 'pr╛C', 'Pref') <> 0 then
  194.                         cycle;
  195.                     CreateResFile(s);
  196.                     if ResError = 0 then
  197.                         i := OpenResFile(s)
  198.                 end;
  199.             until ResError = 0;
  200.         end
  201.         else begin
  202.             GetAppFiles(1, a);
  203.             if SetVol(nil, a.vrefnum) = 0 then
  204.                 i := OpenResFile(a.fname);     {this really should not fail }
  205.         end;
  206.     end;
  207.  
  208. function GetPrefs (request: boolean): boolean;
  209.     var
  210.         d: DialogPtr;
  211.         h: Handle;
  212.         i, j: integer;
  213.         r: Rect;
  214.         m: MenuHandle;
  215.         s: str255;
  216.     begin
  217.         if not ValidPrefs then begin
  218.             default := defaultHndl(GetResource('Pref', 256));
  219.             if default = nil then begin
  220.                 default := defaultHndl(NewClearHandle(sizeof(defaultType)));
  221.                 AddResource(Handle(default), 'Pref', 256, 'ircle Prefs');
  222.                 default^^.port := 6667
  223.             end;
  224.             if gethandlesize(handle(default)) < sizeof(defaultType) then begin
  225.                 sethandlesize(handle(default), sizeof(defaultType));
  226.                 for i := 1 to 4 do
  227.                     default^^.notify[i] := false
  228.             end;
  229.             shortcuts := shortcutsHndl(GetResource('Pref', 257));
  230.             if shortcuts = nil then begin
  231.                 shortcuts := shortcutsHndl(NewClearHandle(sizeof(ShortcutsType)));
  232.                 AddResource(Handle(shortcuts), 'Pref', 257, 'ircle Shortcuts');
  233.             end;
  234.             EnableItem(GetMHandle(M_SHCUTS), 0);
  235.             SetShortcutsMenu;
  236.             defFont := FSHndl(GetResource('Pref', 258));
  237.             if defFont = nil then begin
  238.                 defFont := FSHndl(NewClearHandle(sizeof(FSType)));
  239.                 with defFont^^ do begin
  240.                     fnt := monaco;
  241.                     siz := 9
  242.                 end;
  243.                 AddResource(Handle(defFont), 'Pref', 258, 'ircle Font');
  244.             end;
  245.             with defFont^^ do begin
  246.                 MWdefaultFont := fnt;
  247.                 MWdefaultSize := siz
  248.             end;
  249.             i := ApplTask(@CloseShortcuts, mouseMsg + inGoAway);
  250.             m := GetMHandle(M_FONT);
  251.             EnableItem(m, 0);
  252.             AddResMenu(m, 'FONT');
  253.             DrawMenuBar;
  254.             CheckItem(m, 1, (MWdefaultSize = 9));
  255.             CheckItem(m, 2, (MWdefaultSize = 10));
  256.             CheckItem(m, 3, (MWdefaultSize = 12));
  257.             CheckItem(m, 4, (MWdefaultSize = 14));
  258.             for i := 6 to CountMItems(m) do begin
  259.                 GetItem(m, i, s);
  260.                 GetFNum(s, j);
  261.                 CheckItem(m, i, (j = MWdefaultFont));
  262.             end;
  263.             ApplExitproc(@saveFont);
  264.         end;
  265.         if (default^^.server = '') or (default^^.nick = '') or (default^^.userLoginName = '') then
  266.             request := true;
  267.         if request then begin
  268.             d := GetNewDialog(D_PREFS, nil, WindowPtr(-1));
  269.             defTODia(d);
  270.             repeat
  271.                 ModalDialog(nil, i);
  272.                 if i = 2 then begin
  273.                     DisposDialog(d);
  274.                     GetPrefs := false;
  275.                     exit(GetPrefs)
  276.                 end;
  277.                 if (i >= 9) and (i <= 12) then begin
  278.                     GetDItem(d, i, j, h, r);
  279.                     default^^.notify[i - 8] := not default^^.notify[i - 8];
  280.                     SetCtlValue(ControlHandle(h), ord(default^^.notify[i - 8]));
  281.                 end;
  282.             until i = 1;
  283.             defFROMDia(d);
  284.             DisposDialog(d)
  285.         end;
  286.         GetPrefs := (default^^.server <> '') and (default^^.nick <> '') and (default^^.userLoginName <> '');
  287.         default^^.nick := copy(default^^.nick, 1, 9);
  288.         ChangedResource(Handle(default));
  289.         WriteResource(Handle(default));
  290.     end;
  291.  
  292. procedure GetShortcuts;
  293.     var
  294.         m: MenuHandle;
  295.         i, j: integer;
  296.         h: Handle;
  297.         r: Rect;
  298.     begin
  299.         if shcd = nil then begin
  300.             shcd := GetNewDialog(D_SHCUTS, nil, WindowPtr(-1));
  301.             m := GetMHandle(261);
  302.             for i := 1 to 10 do begin
  303.                 GetDItem(shcd, i, j, h, r);
  304.                 if Shortcuts^^[i] = ' ' then
  305.                     SetIText(h, '')
  306.                 else
  307.                     SetIText(h, Shortcuts^^[i]);
  308.             end;
  309.  
  310.         end
  311.         else
  312.             SelectWindow(shcd);
  313.     end;
  314.  
  315. end.